<template>
  <div class="block">
    <el-table
      :data="tableData"
      style="width: 100%"
      :row-class-name="tableRowClassName"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="name" label="昵称"> </el-table-column>
      <el-table-column prop="username" label="账号"></el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scop">
          <img :src="scop.row.avatar" class="image" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <template>
      <!-- <el-pagination
        background
        class="left"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[100, 200, 300, 400]"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination> -->
      <el-pagination
        background
        class="left"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 2, 3, 100, 200, 300, 400]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </template>
  </div>
</template>

<style>
.el-table .warning-row {
  background: oldlace;
}
.image {
  width: 35%;
  display: block;
}
.el-table .success-row {
  background: #f0f9eb;
}
.left {
  float: left;
}
</style>

<script>
import userAPI from "@/api/sys/user";

export default {
  methods: {
    inintList(pageSize, currentPage) {
      userAPI.getList(pageSize, currentPage).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.total;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.inintList(this.pageSize, this.currentPage);
    },
    handleCurrentChange(val) {
      this.inintList(this.pageSize, val);
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return "warning-row";
      } else if (rowIndex % 2 === 1) {
        return "success-row";
      }
      return "";
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
  },
  data() {
    return {
      tableData: [],
      total: 1000,
      pageSize: 100,
      currentPage: 1,
    };
  },
  created() {
    this.inintList(this.pageSize, this.currentPage);
  },
};
</script>
